import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Demo from "../views/Demo";
import DemoStat from "../views/Stat/DemoStat";
import Login from "../views/Login";
import Tutor from "../views/Tutor";
import TutorMe from "../views/Tutor/TutorMe";
import AdminMe from "../views/Admin/AdminMe";
import TutorRecord from "../views/Tutor/TutorRecord";
import ConsRecord from "../views/Stat/ConsRecord";
import Admin from "../views/Admin";
import AdminRecord from "../views/Admin/AdminRecord";
import AdminDuty from "../views/Admin/AdminDuty";
import AdminCons from "../views/Admin/AdminCons";
import AdminMentor from "../views/Admin/AdminMentor";
import AdminUsr from "../views/Admin/AdminUsr";
import ConsChat from "../views/Stat/ConsChat";
import TutorChat from "../views/Tutor/TutorChat";
import ConsTutor from "../views/Stat/ConsTutor";

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/login',
    component: () => import('../views/Login') //0
  },
  {
    path: '/about', //1
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path:'/users', //2
    name:'人员管理',
    component: () => Demo,
    children:[
      {
        path: '/users/all',
        name: '所有用户',
        component: () =>DemoStat
      },
      {
        path:'/users/add',
        name:'增加用户',
        component: DemoStat
      }
    ]
  },
  {
    path:'/cons', //3
    name:'演示页面',
    component: ()=>Demo,
    children: [
      {
        path:'/cons/me',
        name:'咨询师首页',
        component: ()=>DemoStat
      },
      {
        path:'/cons/record',
        name:'咨询师记录',
        component:()=>ConsRecord
      },
      {
        path:'/cons/chat',
        name:'咨询师通讯',
        component:()=>ConsChat
      },
      {
        path:'/cons/mentorchat',
        name:'求助通讯',
        component:()=>ConsTutor
      }

    ]
  },
  {
    path:'/stat',//4
    name:'统计数据',
    component:()=>Demo,
    children: [
      {
        path: '/stat/all',
        name: '统计',
        component:()=>DemoStat
      }
    ]
  },
  {
    path:'/mentor',//5
    name: '督导页面',
    component:()=>Tutor,
    children: [
      {
        path:'/mentor/me',
        name:'督导首页',
        component:()=>TutorMe
      },
      {
        path:'/mentor/record',
        name:'督导记录',
        component:()=>TutorRecord
      },
      {
        path:'/mentor/chat',
        name:'督导服务',
        component:()=>TutorChat
      }
    ]
  },
  {
    path:'/admin',//6
    name:'管理',
    component:()=>Admin,
    children: [
      {
        path:'/admin/me',
        name:'管理员首页',
        component:()=>AdminMe
      },
      {
        path:'/admin/record',
        name:'咨询记录',
        component:()=>AdminRecord
      },
      {
        path:'/admin/duty',
        name:'排班表',
        component:()=>AdminDuty
      },
      {
        path:'/admin/cons/',
        name:'咨询师管理',
        component:()=>AdminCons
      },
      {
        path:'/admin/mentor',
        name:'督导员管理',
        component:()=>AdminMentor,
      },
      {
        path:'/admin/user',
        name:'来访者管理',
        component:()=>AdminUsr
      }
    ]
  },
  {
    path: '/login',//7
    name: '登录',
    component:()=>Login
  }

]

const router = createRouter({
  base:'/dist/',
  mode:'hash',
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to,from,next)=>{
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})

export default router
